package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.service.EsignFeeService;
import com.xbongbong.paas.service.EsignService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.BaseDTO;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.esign.pojo.dto.EsignFeeChargeDTO;
import com.xbongbong.pro.esign.pojo.dto.EsignFeeLogDTO;
import com.xbongbong.pro.esign.pojo.dto.EsignFeePayStatusDTO;
import com.xbongbong.pro.esign.pojo.dto.EsignFeeQrCodeDTO;
import com.xbongbong.pro.esign.pojo.vo.EsignContractMarginVO;
import com.xbongbong.pro.esign.pojo.vo.EsignFeeChargeVO;
import com.xbongbong.pro.esign.pojo.vo.EsignFeeInfoVO;
import com.xbongbong.pro.esign.pojo.vo.EsignFeeLogVO;
import com.xbongbong.pro.esign.pojo.vo.EsignFeePayStatusVO;
import com.xbongbong.pro.esign.pojo.vo.EsignFeeQrCodeVO;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * e签宝
 * 创建时间 2020/6/17 11:27 AM
 * 修改时间 2020/6/17 11:27 AM
 * @author chy
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/esign/fee")
public class EsignFeeController {

    @Resource
    private EsignFeeService esignFeeService;
    @Resource
    private EsignService esignService;

    /**
     * 查询套餐余量
     * @param baseDTO
     * @param br
     * @return
     * 创建时间 2020/6/18 10:49 AM
     * 修改时间 2020/6/18 10:49 AM
     * @author chy
     */
    @RequestMapping(value = "/getEsignContractMargin", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getEsignContractMargin(@RequestBody @Valid BaseDTO baseDTO, BindingResult br) {
        XbbResponse<EsignContractMarginVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                EsignContractMarginVO esignContractMarginVO = esignService.getEsignContractMargin(baseDTO.getCorpid());
                response = new XbbResponse<>(esignContractMarginVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 获取可充值的e签宝套餐信息
     * @param baseDTO
     * @param br
     * @return
     * 创建时间 2020/6/18 10:49 AM
     * 修改时间 2020/6/18 10:49 AM
     * @author chy
     */
    @RequestMapping(value = "/getInfo", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getInfo(@RequestBody @Valid BaseDTO baseDTO, BindingResult br) {
        XbbResponse<EsignFeeInfoVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                EsignFeeInfoVO esignFeeInfoVO = esignFeeService.getInfo(baseDTO);
                response = new XbbResponse<>(esignFeeInfoVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * e签宝套餐充值
     * @param esignFeeChargeDTO
     * @param br
     * @return
     * 创建时间 2020/6/18 10:49 AM
     * 修改时间 2020/6/18 10:49 AM
     * @author chy
     */
    @RequestMapping(value = "/charge", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String charge(@RequestBody @Valid EsignFeeChargeDTO esignFeeChargeDTO, BindingResult br) {
        XbbResponse<EsignFeeChargeVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                EsignFeeChargeVO esignFeeChargeVO = esignFeeService.charge(esignFeeChargeDTO);
                response = new XbbResponse<>(esignFeeChargeVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 获取e签宝套餐充值状态
     * @param esignFeePayStatusDTO
     * @param br
     * @return
     * 创建时间 2020/6/18 10:49 AM
     * 修改时间 2020/6/18 10:49 AM
     * @author chy
     */
    @RequestMapping(value = "/checkPayStatus", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String checkPayStatus(@RequestBody @Valid EsignFeePayStatusDTO esignFeePayStatusDTO, BindingResult br) {
        XbbResponse<EsignFeePayStatusVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                EsignFeePayStatusVO esignFeePayStatusVO = esignFeeService.checkPayStatus(esignFeePayStatusDTO);
                response = new XbbResponse<>(esignFeePayStatusVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 获取e签宝充值二维码
     * @param esignFeeQrCodeDTO
     * @param br
     * @return
     * 创建时间 2020/6/18 10:50 AM
     * 修改时间 2020/6/18 10:50 AM
     * @author chy
     */
    @RequestMapping(value = "/getQRCode", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getQRCode(@RequestBody @Valid EsignFeeQrCodeDTO esignFeeQrCodeDTO, BindingResult br) {
        XbbResponse<EsignFeeQrCodeVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                EsignFeeQrCodeVO esignFeeQrCodeVO = esignFeeService.getQRCode(esignFeeQrCodeDTO);
                response = new XbbResponse<>(esignFeeQrCodeVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }


    /**
     * 获取充值记录列表
     * @param esignFeeLogDTO
     * @param br
     * @return
     * 创建时间 2020/6/18 10:51 AM
     * 修改时间 2020/6/18 10:51 AM
     * @author chy
     */
    @RequestMapping(value = "/log/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String logList(@RequestBody @Valid EsignFeeLogDTO esignFeeLogDTO, BindingResult br) {
        XbbResponse<EsignFeeLogVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                EsignFeeLogVO esignFeeLogVO = esignFeeService.logList(esignFeeLogDTO);
                response = new XbbResponse<>(esignFeeLogVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
}
